package com.one.group.admin.controller;

import com.one.group.enums.ErrorCode;
import com.one.group.model.entity.QuestionOption;
import com.one.group.result.Result;
import com.one.group.service.QuestionOptionService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author kld
 * @date 2025/10/15 18:41
 * @description 选项
 */
@RestController
@RequestMapping("/questionOption")
@Tag(name="选项管理")
public class QuestionOptionController {
    @Autowired
    private QuestionOptionService questionOptionService;
    /**
     * 新增选项
     */
    @PostMapping("/add")
    public Result add(@RequestBody QuestionOption questionOption){
        if(questionOption.getQuestionId() == null){
            return new ResultUtil().error(300,"问题id不能为空");
        }
        if(questionOption.getContent() == null || questionOption.getContent().trim().isEmpty()){
            return new ResultUtil().error(300,"选项内容不能为空");
        }

        questionOptionService.addOption(questionOption);

        return  new ResultUtil().success();
    }
    /**
     * 删除选项
     */
    @GetMapping("/delete")
    public Result delete(Long optionId){
        if(questionOptionService.deleteOption(optionId)){
            return ResultUtil.success("删除选项成功");
        } else {
            return ResultUtil.error(ErrorCode.OPERATION_ERROR, "删除选项失败或选项不存在");
        }
    }
    /**
     * 删除问题下的所有选项
     */
    @GetMapping("/deleteAll")
    public Result deleteAll(Long questionId){
        if(questionOptionService.deleteOptionsByQuestionId(questionId)){
            return ResultUtil.success("删除问题下的所有选项成功");
        } else {
            return ResultUtil.error(ErrorCode.OPERATION_ERROR, "删除问题下的所有选项失败或问题不存在");
        }
    }
    /**
     * 获取问题下的所有选项
     */
    @GetMapping("/getAll")
    public Result getAll(Long questionId){
        return ResultUtil.success(questionOptionService.getOptionsByQuestionId(questionId));
    }
    /**
     * 获取选项
     */
    @GetMapping("/get")
    public Result get(Long optionId){
        return ResultUtil.success(questionOptionService.getById(optionId));
    }
    /**
     * 更新选项
     */
    @PostMapping("/update")
    public Result update(@RequestBody QuestionOption questionOption){
        return ResultUtil.success(questionOptionService.updateOption(questionOption));
    }
    
    /**
     * 更新选项排序
     */
    @GetMapping("/updateSort")
    public Result updateSort(Long questionId) {
        if (questionId == null) {
            return ResultUtil.error(ErrorCode.PARAMS_ERROR, "问题ID不能为空");
        }
        // 实现根据问题ID对选项进行排序
        questionOptionService.batchUpdateSort(questionId);
        return ResultUtil.success("选项排序更新成功");
    }
}
